package com.ruoyi.web.core.config.mqtt.consumer;

import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @Author wy
 * @Description 设备端发布数据回调
 * @Date 2023/11/22 16:08
 * @Version 1.0
 */
@Log4j2
public class MqttConsumerDeviceReplyCallBack implements MqttCallback {
    /**
     *  与服务器断开连接，可重连
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
      log.error("---MqttConsumerDeviceCallBack与服务器断开连接----");
      log.error(throwable);
    }

    /**
     * 消息到达的回调
     * @param topic
     * @param mqttMessage
     * @throws Exception
     */
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        //wisezone/device/reply/A01A06A01/CQ101
        System.out.println(String.format("接收消息主题 : %s",topic));
        System.out.println(String.format("接收消息Qos : %d",mqttMessage.getQos()));
        System.out.println(String.format("接收消息内容 : %s",new String(mqttMessage.getPayload())));
        System.out.println(String.format("接收消息retained : %b",mqttMessage.isRetained()));
    }

    /**
     * 消息发布成功的回调
     * @param iMqttDeliveryToken
     */
    @SneakyThrows
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
       log.info(iMqttDeliveryToken.getMessage());
    }
}
